package com.demo.mvc.ioc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;


@Slf4j
@Component
public class People005 implements ApplicationContextAware {

  Animal animal;
  ApplicationContext applicationContext;

//  @Autowired
//  public void setAnimal(  @Qualifier("dogImpl") Animal aAnimal){
//    animal = aAnimal;
//  }


  public void setAnimal(){
    Map<String, Animal> objectObjectHashMap = applicationContext.getBeansOfType(Animal.class);
    for(Map.Entry<String, Animal> animal : objectObjectHashMap.entrySet()){
      log.info("test123,{}", animal.getClass());
      if (CatImpl.class.getName().equals(animal.getValue().getClass().getName())){
        this.animal= animal.getValue();
        break;
      }
    }
  }


  public void myAnimalSay(){
    animal.say();
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }
}
